//
// Calc.cpp for 207 in /home/druart_p/Work/math/my-math-epi/207
//
// Made by pierrick druart
// Login   <druart_p@epitech.net>
//
// Started on  Wed May 23 11:16:53 2012 pierrick druart
// Last update Wed May 23 14:41:53 2012 pierrick druart
//

#include	<math.h>

#include	"Calc.hh"

// CONSTRUCTOR & DESTRUCTOR

Calc::Calc()
{
  this->coef_x = 0;
  this->coef_y = 0;
  this->coef_x2 = 0;
  this->coef_y2 = 0;
  this->res = 0;
  this->res2 = 0;
}

Calc::~Calc() {}

// GETTER & SETTER

double		Calc::getAdeX() const
{
  return (this->coef_x);
}

double		Calc::getBdeX() const
{
  return (this->coef_y);
}

double		Calc::getAdeY() const
{
  return (this->coef_x2);
}

double		Calc::getBdeY() const
{
  return (this->coef_y2);
}


// CALCUL

void		Calc::coefx_to_y(std::list<int> boy, std::list<int> girl)
{
  std::list<int>::iterator	it;
  std::list<int>::iterator	it2;
  int			total = 0;
  double		x = 0;
  double		xx = 0;
  double		y = 0;
  double		yy = 0;

  it2 = girl.begin();
  for (it = boy.begin(); it != boy.end(); ++it)
    {
      x += *it2;
      xx += pow(*it2, 2);
      y += *it;
      yy += *it * *it2;
      it2++;
      total++;
    }
  this->coef_x = (y * xx - x * yy) / (total * xx - pow(x, 2));
  this->coef_y = (total * yy - x * y) / (total * xx - pow(x, 2));
  ecartx_to_y(boy, girl, total);
}

void		Calc::coefy_to_x(std::list<int> boy, std::list<int> girl)
{
  std::list<int>::iterator	it;
  std::list<int>::iterator	it2;
  int			total = 0;
  double		x = 0;
  double		xx = 0;
  double		y = 0;
  double		yy = 0;

  it2 = boy.begin();
  for (it = girl.begin(); it != girl.end(); ++it)
    {
      x += *it2;
      xx += pow(*it2, 2);
      y += *it;
      yy += *it * *it2;
      it2++;
      total++;
    }
  this->coef_x2 = (y * xx - x * yy) / (total * xx - pow(x, 2));
  this->coef_y2 = (total * yy - x * y) / (total * xx - pow(x, 2));
  ecarty_to_x(boy, girl, total);
}

void		Calc::ecartx_to_y(std::list<int> boy, std::list<int> girl, int total)
{
  std::list<int>::iterator	it;
  std::list<int>::iterator	it2;
  double			ecart = 0;

  it2 = girl.begin();
  for (it = boy.begin(); it != boy.end(); ++it)
    {
      ecart += pow((*it) - (this->coef_x + this->coef_y * (*it2)), 2);
      it2++;
    }
  this->res = sqrt(ecart / (double)total);
  std::cout << "Regression de Y sur X" << std::endl;
  std::cout << "a = " << this->coef_x << std::endl;
  std::cout << "b = " << this->coef_y << std::endl;
  std::cout << "s = " << this->res << std::endl;
}

void		Calc::ecarty_to_x(std::list<int> boy, std::list<int> girl, int total)
{
  std::list<int>::iterator	it;
  std::list<int>::iterator	it2;
  double			ecart = 0;

  it2 = boy.begin();
  for (it = girl.begin(); it != girl.end(); ++it)
    {
      ecart += pow((*it - (this->coef_x2 + this->coef_y2 * *it2)), 2);
      it2++;
    }
  this->res2 = sqrt(ecart / (double)total);
  std::cout << "Regression de Y sur X" << std::endl;
  std::cout << "a = " << this->coef_x2 << std::endl;
  std::cout << "b = " << this->coef_y2 << std::endl;
  std::cout << "s = " << this->res2 << std::endl;
  this->total = total;
}

void		Calc::correl(std::list<int> boy, std::list<int> girl)
{
  std::list<int>::iterator	it;
  std::list<int>::iterator	it2;
  double			r = 0;
  double			s_mul = 0;
  double			x = 0;
  double			y = 0;
  double			s_boy = 0;
  double			s_girl = 0;
  double			s_boy2 = 0;
  double			s_girl2 = 0;

  it2 = boy.begin();
  for (it = girl.begin(); it != girl.end(); ++it)
    {
      s_boy2 += *it2;
      s_girl2 += *it;
      it2++;
    }
  it2 = boy.begin();
  x = (s_boy2 / this->total);
  y = (s_girl2 / this->total);
  for (it = girl.begin(); it != girl.end(); ++it)
    {
      s_mul += (*it2 - x) * (*it - y);
      s_boy += pow(*it2 - x, 2);
      s_girl += pow(*it - y, 2);
      it2++;
    }
  r = ((s_mul) / (sqrt(s_boy) * sqrt(s_girl)));
  std::cout << "Coefficient de correlation" << std::endl;
  std::cout << "r = " << r << std::endl;
}
